<?php
/**
 * Created by PhpStorm.
 * User: mac1
 * Date: 16/6/10
 * Time: 上午10:32
 */

namespace app\api\controller;
use think\Db;

class Meeting extends Base
{

    /**
     * 创建会议（新建的时候显示好友列表就直接调用friend里的好友列表即可）
     *
     * @return string
     */
    public function createMeet(){
        extract(input());
		$uid = explode(',',trim($uid,','));
        if(!$uid){
            return json_encode(['status'=>0,'msg'=>'请选择用户']);
        }
        if(count($uid) >= 10){
            return json_encode(['status'=>0,'msg'=>'最多只能选9人']);
        }
        $uidArray = $uid;
        $uid = implode($uid,',');
        $uid = trim($uid,',');
        $uid = ',' . $uid . ',';
        $data = array();
        $data['m_name'] = $name;
        $data['m_id'] = $this->uuid();
        $data['m_user_id'] = $user_id;
        $data['m_uids'] = $uid;
        $data['m_status'] = 1;
        $data['m_starttime'] = $starttime . ':00';
        $data['m_time'] = date('Y-m-d H:i:s');
        if($data['m_starttime'] <= $data['m_time']){
            $data['m_status'] = 2;
        }
        Db::startTrans();
        try {
            $meetid = $data['m_id'];
            Db::table('up_meeting')->insert($data);
            $status = 200;//这俩看声网
            $msg = $meetid;//这俩看声网
//            $uids = '';
//            foreach ($uidArray as $v) {
//                $uids .= "userId={$v}&";
//            }
//            $res = $this->create($uids,$meetid,$name);
//            if ($res['code'] != 200) {
//                Db::table('up_meeting')->where('m_id', $meetid)->delete();
//                $status = 0;
//                $msg = '新增失败';
//            }else{
//                $status = 200;
//                $msg = $meetid;
//            }
            Db::commit();
            $returnData['status'] = $status;
            $returnData['msg'] = $msg;
            return json_encode($returnData);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $returnData['status'] = 0;
            $returnData['msg'] = "新增失败！";
            return json_encode($returnData);
        }
    }

    /**
     * 我的会议
     *
     * @return string
     */
    public function myMeet(){
        extract(input());
//        $uid = ',' . $user_id . ',';
        $now = date('Y-m-d H:i:s');
        $change = Db::table('up_meeting')
            ->where('m_status', 1)
            ->where('m_starttime','<=',$now)
            ->setField('m_status',2);
        $res = Db::table('up_meeting')
            ->where('m_uids', 'like',"%,$user_id,%")
            ->where('m_status', 'in','1,2')
            ->order('m_status asc,m_starttime asc')
            ->page($page,15)
            ->select();
        if($res){
            foreach ($res as $k=>$v){
                $uids = trim($v['m_uids'],',');
                $res[$k]['images'] = Db::table('up_u_user')
                    ->where('user_id', 'in',$uids)
//                    ->where('user_id', '<>',$user_id)
                    ->column('user_image');
            }
        }
        return json_encode(['status'=>200,'msg'=>$res]);
    }
    /**
     * 我的某个会议详情
     * （只有发起人才可以取消）
     * @return string
     */
    public function myMeetInfo(){
        extract(input());
        $res = Db::table('up_meeting')
            ->where('m_id', $m_id)
            ->find();
        $uids = trim($res['m_uids'],',');
        $images = Db::table('up_u_user')
            ->where('user_id', 'in',$uids)
            ->field('user_id,user_username,user_image')
            ->select();
        return json_encode(['status'=>200,'msg'=>$res,'images'=>$images]);
    }
    /**
     * 取消
     *
     * @return string
     */
    public function cancelMeet(){
        extract(input());
        $own = Db::table('up_meeting')->where('m_id', $m_id)->value('m_user_id');
        if($own != $user_id){
            return json_encode(['status'=>100,'msg'=>'只有发起人才可以操作！']);
        }
        $res = Db::table('up_meeting')
            ->where('m_id', $m_id)
            ->setField('m_status',3);
        if($res){
            return json_encode(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json_encode(['status'=>100,'msg'=>'操作失败，请刷新重试']);
        }

    }

    //----------------------
    public function videoOnOffUser(){
        extract(input());
        $have = Db::table('up_meeting_video')
            ->where('mv_m_id', $m_id)
            ->where('mv_user_id', $user_id)
            ->find();
        if($have){
            $res = Db::table('up_meeting_video')
                ->where('mv_m_id', $m_id)
                ->where('mv_user_id', $user_id)
                ->update([
                    'mv_status' => $status,
                    'mv_time' => date('Y-m-d H:i:s'),
                ]);
        }else{
            $data = [
                'mv_m_id' => $m_id,
                'mv_user_id' => $user_id,
                'mv_status' => $status,
                'mv_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_meeting_video')->insert($data);
        }
        if($res){
            return json_encode(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json_encode(['status'=>100,'msg'=>'操作失败，请刷新重试']);
        }

    }
    public function getWhoOn(){
        extract(input());
        $m_uids = Db::table('up_meeting')
            ->where('m_id', $m_id)
            ->value('m_uids');

        $uids = str_replace(",$user_id,",",",$m_uids);
        $uids = trim($uids,',');
        $data = Db::table('up_u_user')
            ->where('user_id', 'in',$uids)
            ->field('user_id,user_username,user_image')
            ->select();
        $open_ids = Db::table('up_meeting_video')
            ->where('mv_m_id', $m_id)
            ->where('mv_status', 1)
            ->column('mv_user_id');
        $t_u = [];
        foreach ($data as $k => $v){
            $data[$k]['open'] = 2;
            if(in_array($v['user_id'],$open_ids)){
                $data[$k]['open'] = 1;
            }
            $t_u['user_id'.$v['user_id']] = $data[$k]['open'];

        }
        return json_encode(['status'=>200,'msg'=>$data,'t_u'=>$t_u]);
    }
}